package com.clz.appdesign.myapp.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.clz.appdesign.appdependence.base.BaseActivity;
import com.clz.appdesign.myapp.R;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author chenliangzhi
 * @date 2018/4/24
 * @describe
 */

public class NotificationActivity extends BaseActivity {
    @Override
    public void initToolbar() {
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness(Context mContxt) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channleId = "chat";
            String channelName = "聊天消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channleId, channelName, importance);
            channleId = "subscribe";
            channelName = "订阅消息";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channleId, channelName, importance);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setShowBadge(true);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);

    }

    @Override
    public int layoutResID() {
        return R.layout.activity_notification;
    }


    @OnClick({R.id.btn_send_chat, R.id.btn_send_subscribe})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_send_chat:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    sendChatMsg();
                }
                break;
            case R.id.btn_send_subscribe:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    sendSubscribeMsg();
                }
                break;
            default:
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void sendChatMsg() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final NotificationChannel chatChannel = manager.getNotificationChannel("chat");
            if (chatChannel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(android.provider.Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, chatChannel.getId());
                startActivity(intent);
                ToastUtils.showShort("请手动将通知打开");
            }
        }
        Intent intent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationCompat.Builder(this, "chat")
                .setContentTitle("收到一条聊天消息")
                .setContentText("今天中午吃什么?")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setNumber(1)
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(1, notification);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void sendSubscribeMsg() {
        Intent intent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("收到一条订阅消息")
                .setContentText("地铁沿线30万商铺火爆售卖中!!")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(2, notification);
    }
}
